package com.loyal.qr.zbar;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

import com.loyal.qr.CodeUtil;
import com.loyal.qr.R;

/**
 * 一维码/条形码
 */
public class BarCodeView extends LinearLayoutCompat {
    private ImageView imageView;
    private TextView textView;
    private String barContent;
    private float textSize = 12f;
    private int textColor = Color.BLACK;
    private boolean boldText = false;
    private Context context;

    public BarCodeView(Context context) {
        super(context);
        initialize(context, null);
    }

    public BarCodeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public BarCodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        this.context = context;
        TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.BarCodeView);
        textSize = attributes.getDimension(R.styleable.BarCodeView_android_textSize, dp2px(textSize));
        textColor = attributes.getColor(R.styleable.BarCodeView_android_textColor, textColor);
        barContent = attributes.getString(R.styleable.BarCodeView_android_text);
        boldText = attributes.getBoolean(R.styleable.BarCodeView_boldText, boldText);
        //getDimensionPixelSize()方法返回的实际数值是四舍五入的。
        //getDimensionPixelOffset返回的实际数值是去掉结果后面的小数点。
        int marginTop = attributes.getDimensionPixelSize(R.styleable.BarCodeView_textMarginTop, -10);
        int marginBottom = attributes.getDimensionPixelSize(R.styleable.BarCodeView_textMarginBottom, 0);
        setOrientation(VERTICAL);

        initBitmapView();
        initTextView();

        setMargins(0, marginTop, 0, marginBottom);
        attributes.recycle();
    }

    private void initBitmapView() {
        imageView = new ImageView(context);
        Bitmap bitmap = CodeUtil.createBarBitmap(barContent);
        imageView.setImageBitmap(bitmap);
        addView(imageView);
    }

    private void initTextView() {
        textView = new TextView(context);
        setBoldText(boldText);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setTextColor(textColor);
        textView.setSingleLine(true);
        textView.setMaxLines(1);
        textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
        textView.setText(barContent);

        addView(textView);
    }

    public void setBarCode(String content) {
        this.barContent = content;
        if (null != textView)
            textView.setText(content);
        Bitmap bitmap = CodeUtil.createBarBitmap(content);
        if (null != imageView)
            imageView.setImageBitmap(bitmap);
    }

    public void setBarCode(String content, int bmpWidth, int bmpHeight) {
        this.barContent = content;
        if (null != textView)
            textView.setText(content);
        Bitmap bitmap = CodeUtil.createBarBitmap(content, bmpWidth, bmpHeight);
        if (null != imageView)
            imageView.setImageBitmap(bitmap);
    }

    public String getBarContent() {
        return barContent;
    }

    public void setTextSize(float textSize) {
        if (null != textView)
            textView.setTextSize(textSize);
    }

    public void setTextColor(@ColorInt int color) {
        if (null != textView)
            textView.setTextColor(color);
    }

    public void setBoldText(boolean boldText) {
        if (null != textView) {
            Typeface typeface = boldText ? Typeface.defaultFromStyle(Typeface.BOLD) :
                    Typeface.defaultFromStyle(Typeface.NORMAL);
            textView.setTypeface(typeface);
        }
    }

    public void setMargins(int left, int top, int right, int bottom) {
        if (null != textView) {
            LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
            layoutParams.setMargins(left, top, right, bottom);
            textView.setLayoutParams(layoutParams);
        }
    }

    public TextView getContentView() {
        return textView;
    }

    public ImageView getBitmapView() {
        return imageView;
    }

    public static float dp2px(float dp) {
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        return dp * metrics.density;
    }
}
